<?php
/*********************************************
  Copyright (c) 2011 by DragonflyCMS
  http://dragonflycms.org
  Released under GNU GPL version 2 or any later version
**********************************************/

abstract class PHP
{

	public static
		$version,
		$error_log,
		$sapi,
		$asNobody;

	private static $rollback = array();

	public static function init()
	{
		self::runAs();
		self::$version = (int) preg_replace('#^(\d)\.(\d).*#', '$1$2', phpversion());
		if ($disabled = ini_get('disable_functions')) {
			trigger_error('Host provider disabled the following functions: '. $disabled , E_USER_NOTICE);
		}
		self::$asNobody = _PROCESS_OWNER == 'nobody' || getmyuid() != fileowner($file);

		//global $Shutdown;
		//$Shutdown->register(array('PHP', 'restore'));
		// Needs to register to DF on BOOT_DOWN
		$settings = array(
			'default_socket_timeout' => 7,
			'display_errors' => DF_MODE_DEVELOPER,
			'display_startup_errors' => DF_MODE_DEVELOPER, // should work in cgi
			'mysql.connect_timeout' => 7,
			'user_agent' => 'DragonflyCMS/10 (PHP '.PHP_MAJOR_VERSION.'; '.PHP_SAPI.'; '.PHP_OS.')',
		);
		if (POODLE_CLI) {
			$settings['display_errors']     = DF_MODE_DEVELOPER ? 'stderr' : 0;
		} else {
			$settings['max_execution_time'] = 30;
			$settings['url_rewriter.tags']  = 'nourlrewrite';
		}
		foreach ($settings as $cfg => $val) {
			\Poodle\PHP\INI::set($cfg, $val);
		}
		if (!DF_LOG_FILE) {
			\Poodle\PHP\INI::set('log_errors', 0);
		} else {
			\Poodle\PHP\INI::set('error_log', DF_LOG_FILE); // it should check if file is writable
			\Poodle\PHP\INI::set('ignore_repeated_errors', 1);
			\Poodle\PHP\INI::set('log_errors', 1);
			\Poodle\PHP\INI::set('log_errors_max_len', 255);
		}
		//self::$version > 53 ini_set('mail.log', ''); // very cool, also verifing mailing lists?
	}

	# return false on failure, mixed on success
	# also saves config changes to rollback
	public static function set($cfg, $val)
	{
		return \Poodle\PHP\INI::set($cfg, $val);
	}

	public static function get($cfg)
	{
		$val = trim(ini_get($cfg));
		# "", "0", null, "Off"
		if (!$val || 'off' == strtolower($val)) return false;
		# "1", true, "On"
		if (1 == $val || 'on' == strtolower($val)) return true;
		# digits
		if (ctype_digit($val)) return intval($val);
		# strings and floats
		return $val;
	}

	public static function restore()
	{
		\Poodle\PHP\INI::restore();
		return true;
	}

	private static function runAs()
	{
		/* aolserver apache apache2filter apache2handler caudium cgi cgi-fcgi(cli-cgi) cli continuity
		   embed isapi milter nsapi phttpd pi3web roxen thttpd tux webjames
		*/
		switch (true):
			case 'cli' === PHP_SAPI:
				#[REQUEST_METHOD] => CLI
				#[HTTP_USER_AGENT] => Mozilla/5.0 (cli; U; WINNT; en) PHP_CLI/5.3.19
				self::$sapi = 'cli';
				print 'Mode not supported, yet.';
				exit(1);
				break;
			case 'cgi-fcgi' === PHP_SAPI && empty($_SERVER['REMOTE_ADDR']):
				self::$sapi = 'cli-cgi';
				print 'Mode not supported.';
				exit(1);
				break;
			case false !== stripos(PHP_SAPI, 'cgi'):
				self::$sapi = 'http-cgi';
				break;
			default:
				self::$sapi = 'http';
				break;
		endswitch;
	}
}

PHP::init();
//			print_r($_SERVER);
